﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DevToolsRepo
{
	public partial class MainForm : Form
	{
		private ToolRepository _repo = ToolRepository.Instance;
		public MainForm()
		{
			InitializeComponent();
			_repo.Load();

			if (_repo.Tools.Count <= 0)
			{
				for (int i = 0; i < 10; i++)
				{
					_repo.Tools.Add(new ToolInfo { Name = "Test " + i });
				}
			}
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			LoadList();
		}

		#region [ LoadList ]
		public void LoadList()
		{
			dgTools.DataSource = null;
			dgTools.DataSource = _repo.Tools;
		}
		#endregion

		#region [ mnuAdd_Click ]
		private void mnuAdd_Click(object sender, EventArgs e)
		{
			var toolInfo = new ToolInfo();
			if (new frmEditTool(toolInfo, true).ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				ToolRepository.Instance.Tools.Add(toolInfo);
				LoadList();
			}
		}
		#endregion

		#region [ mnuEdit_Click ]
		private void mnuEdit_Click(object sender, EventArgs e)
		{
			var row = dgTools.CurrentRow.DataBoundItem as ToolInfo;
			if (row != null)
			{
				new frmEditTool(row, false).ShowDialog();
				LoadList();
			}
		}
		#endregion

		#region [ mnuDelete_Click ]
		private void mnuDelete_Click(object sender, EventArgs e)
		{

		}
		#endregion

		#region [ MainForm_FormClosing ]
		private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			_repo.Save();
		} 
		#endregion
	}
}
